<?php
//页面
class pageclass
{
	var $DB;
	var $prefix;
	var $bi;
	
	function __construct($DB,$prefix="qinggan_",$bi)
	{
		$this->DB = $DB;
		$this->prefix = $prefix;
		$this->bi = $bi;
	}

	//兼容PHP4
	function pageclass($DB,$prefix="qinggan_",$bi)
	{
		$this->__construct($DB,$prefix,$bi);
	}
	
	//当前分类信息
	function thiscate($id)
	{
		$bi = $this->bi;
		$sql = "SELECT c.*,s.sign AS sysgroup_sign FROM ".$this->prefix."category AS c,".$this->prefix."sysgroup AS s WHERE c.id='".$id."' AND c.sysgroupid=s.id AND c.status=1";
		$thiscate = $this->DB->qgGetOne($sql);
		if($thiscate)
		{
			$thiscate["catename"] = $thiscate["catename".$bi];
			$thiscate["note"] = $thiscate["note".$bi];
			if($thiscate["thumb"] || $thiscate["thumb2"] || $thiscate["thumb3"])
			{
				$thiscate = $this->add_thumbarr($thiscate);
			}
			return $thiscate;
		}
	}
	
	//上一级分类信息
	function upcate($id)
	{
		$sql = "SELECT * FROM ".$this->prefix."category WHERE id='".$id."'";
		$rs = $this->DB->qgGetOne($sql);
		$sql = "SELECT * FROM ".$this->prefix."category WHERE id='".$rs["parentid"]."'";
		$upcate = $this->DB->qgGetOne($sql);
		if($upcate["thumb"] || $upcate["thumb2"] || $upcate["thumb3"])
		{
			$upcate = $this->add_thumbarr($upcate);
		}
		return $upcate;
	}
	
	//子分类信息
	function soncate($id)
	{
		$sql = "SELECT id,thumb,sysgroupid,rootid,parentid,catename".$this->bi." FROM ".$this->prefix."category WHERE status='1' AND (rootid='".$id."' OR id='".$id."' OR parentid='".$id."') ORDER BY taxis ASC,id DESC";
		$sonlist = $this->DB->qgGetAll($sql);
		return $sonlist;
	}
	
	//获取总数
	function msgcount($id,$ifnew,$where)
	{
		$sql = "SELECT count(*) FROM ".$this->prefix."msg AS m LEFT JOIN ".$this->prefix."category AS c ON m.cateid=c.id LEFT JOIN ".$this->prefix."msg_content AS msg ON (m.id=msg.id) ".$this->condition($id,$ifnew,$where);
		$msgcount = $this->DB->qg_count($sql);
		return $msgcount;
	}
	
	//当前分类内容
	function msglist($id,$cut1,$cut2,$offset,$psize,$ifnew,$where)
	{
		$sql = "SELECT m.*,c.catename,c.catename_en,c.parentid,msg.content,msg.content_en,msg.content_ft FROM ".$this->prefix."msg AS m INNER JOIN ".$this->prefix."category AS c ON m.cateid=c.id LEFT JOIN ".$this->prefix."msg_content AS msg ON (m.id=msg.id) ".$this->condition($id,$ifnew,$where)." ORDER BY ".$this->sqlorder($id)." LIMIT ".$offset.",".$psize;
		$rslist = $this->DB->qgGetAll($sql);
		//获取图片
		foreach($rslist AS $key=>$value)
		{
			$value = $this->add_thumbarr($value);
			$rslist[$key] = $value;
		}
		//获取系统组自定义图片尺寸
		$sysimg = $this->sysize($id);
		$swidth = $sysimg["width"];
		$sheight = $sysimg["height"];
		$rslist = $this->msgcontent($id,$rslist,$cut1,$cut2,$swidth,$sheight);
		return $rslist;
	}

	//根据分类id获取系统组图片尺寸
	function sysize($cateid)
	{
		//获取系统组信息
		$rs = $this->getroot($cateid);
		$sysgroup = $this->rs_sysgroup($rs["sysgroupid"]);
		//获取系统组自定义图片尺寸
		$sysimg = picsize($sysgroup["id"]);
		return $sysimg;
	}

	//系统组信息
	function rs_sysgroup($id="",$sign="")
	{
		if($id)
		{
			$sql = "SELECT * FROM ".$this->prefix."sysgroup WHERE id='".$id."'";
			$rs = $this->DB->qgGetOne($sql);
			return $rs;
		}
		else
		{
			if($sign)
			{
				$sql = "SELECT * FROM ".$this->prefix."sysgroup WHERE sign='".$sign."'";
				$rs = $this->DB->qgGetOne($sql);
				return $rs;
			}
		}
	}
	
	//详细内容
	function msgcontent($id,$rslist,$cut1,$cut2,$swidth,$sheight)
	{
		if($rslist)
		{
			$bi = $this->bi;
			$i = 1;
			foreach($rslist AS $key=>$value)
			{
				//转换语言版本
				$value["catename"] = $value["catename".$bi];
				$value["subject"] = $value["subject".$bi];
				if($cut1)
				{
					$value["cut_subject"] = CutString($value["subject"],$cut1);
				}
				
				//月份转换
				$value["day"] = date("d",$value["postdate"]);
				$value["month"] = monthc(date("m",$value["postdate"]),"min"); //[min] or [ful]
				$value["year"] = date("Y",$value["postdate"]);
				$value["postdate"] = date("Y.m.d",$value["postdate"]);
				
				//裁剪内容字数
				$value["content"] = $value["content".$bi];
				if($cut2)
				{
					$value["cut_content"] = strip_tags($value["content"]);
					$value["cut_content"] = CutString(str_replace(array("<p>","</p>","&nbsp;","<br />","　"),array("","","","",""),$value["cut_content"]),$cut2);
				}
				//获取自定义字段
				$value["fieldlist"] = $this->fieldlist($value["id"]);
				if($value["fieldlist"])
				{
					foreach($value["fieldlist"] AS $key2=>$value2)
					{
						$value[$value2["sign"]] = $value2["field_content"];
					}
				}
				
				//裁剪图片
				$value["cutimg"] = "images.php?imgurl=".base64_encode($value["folder"].$value["filename"])."&swidth=".$swidth."&sheight=".$sheight;
				
				$value["row_index"] = $i;
				$i ++;
				$msglist[] = $value;
			}
			return $msglist;
		}
	}
	
	//排序方式
	function sqlorder($id)
	{
		$rs = $this->thiscate($id);
		if($rs["parentid"]==0)
		{
			$sqlorder = "m.istop DESC,m.allsortid asc,m.id desc";
		}
		else
		{
			$sqlorder = "m.istop DESC,m.sortid asc,m.id desc";
		}
		return $sqlorder;
	}
	
	//条件
	function condition($id,$ifnew,$where)
	{
		$condition = "WHERE m.ifcheck='1'";
		//普通产品
		if($ifnew==0)
		{
			$condition .= " AND m.isvouch ='0'";
		}
		//新品
		elseif($ifnew==1)
		{
			$condition .= " AND m.isvouch ='1'";
		}
		//全部
		elseif($ifnew==2)
		{
			$condition .= "";
		}
		//兼容搜索页
		$idin = explode(",",$id);
		if($idin[1])
		{
			$idin = $id;
		}
		else
		{
			unset($idin);
			//获取子分类
			$sonlist = $this->soncate($id);
			foreach($sonlist AS $key=>$value)
			{
				$idin .= $value["id"];
				if($key != count($sonlist)-1)
				{
					$idin .= ",";
				}
			}
			if(!$idin)
			{
				$idin = $id;
			}
		}
		$condition .= " AND m.subject".$this->bi."!=''";
		$condition .= " AND m.cateid IN(".$idin.")";
		$condition .= " AND m.additonid='0'";
		$bi = $this->bi;
		if($where)
		{
			$keywords = $where["like"];
			$condition .= "AND (m.subject".$bi." LIKE '%".$keywords."%' OR c.catename".$bi." LIKE '%".$keywords."%' OR msg.content".$bi." LIKE '%".$keywords."%')";
		}
		return $condition;
	}
	
	//自定义字段
	function fieldlist($msgid="",$cateid="",$ifdata=true)
	{
		if($msgid){
			$fieldgroupid = upfieldid($msgid);
		}
		else{
			$fieldgroupid = field_cateid($cateid);
		}
		//$fieldgroupid = 0;
		$rs = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."sysgroup_fieldgroup WHERE id='".$fieldgroupid."'");
		//读取字段组字段
		if($rs["fieldlist"])
		{
			$fieldids = explode(",",$rs["fieldlist"]); 
			foreach($fieldids AS $key=>$value)
			{
				$rs = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."sysgroup_field WHERE id='".$value."' AND ifcheck=1 ORDER BY sortid ASC,id ASC");
				if($rs)
				{
					$fieldarray[] = $rs;
				}
			}
		}
		//读取全部字段
		else
		{
			if($cateid){
				$rs = $this->getroot($cateid);
				$sysgroup = $this->rs_sysgroup($rs["sysgroupid"]);
			}
			else{
				$cate = $this->DB->qgGetOne("SELECT cateid FROM ".$this->prefix."msg WHERE id='".$msgid."'");
				$rs = $this->getroot($cate["cateid"]);
				$sysgroup = $this->rs_sysgroup($rs["sysgroupid"]);
			}
			$fieldarray = $this->DB->qgGetAll("SELECT * FROM ".$this->prefix."sysgroup_field WHERE sysgroupid='".$sysgroup["id"]."' AND ifcheck=1 ORDER BY sortid ASC,id ASC");
		}
		//数据处理
		foreach($fieldarray AS $key=>$value)
		{
			$value["field_name"] = $value["field_name".$this->bi];
			if(!$value["field_name"])
			{
				if($value["field_name_en"])
				{
					$value["field_name"] = $value["field_name_en"];
				}
				if($value["field_name_ft"])
				{
					$value["field_name"] = $value["field_name_ft"];
				}
			}
			
			//选项类添加选项数组
			if($value["field_type"]=="select" || $value["field_type"]=="checkbox")
			{
				$options = explode(",",$value["field_option"]);
				foreach($options AS $key2=>$value2)
				{
					$value3["inputval"] = $value2;
					$value["options"][] = $value3;
				}
				unset($options,$key2,$value2,$value3);
			}
			$fieldarray[$key] = $value;
		}
		//关联msg_field表及数据处理
		if($msgid && $ifdata)
		{
			if($fieldarray)
			{
				foreach($fieldarray AS $key=>$value)
				{
					$rs = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."msg_field WHERE fieldid='".$value["id"]."' AND msgid='".$msgid."'");
					if($value["ifshare"]==1)
					{
						$rs["field_content_en"] = $rs["field_content_ft"] = $rs["field_content"];
						
						//附件数据添加
						if($value["field_type"]=="file")
						{
							$value["file"] = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."upfiles WHERE id='".$rs["field_content"]."'");
						}
					}
					else
					{
						$rs["field_content"] = $rs["field_content".$this->bi];
						
						//附件数据添加
						if($value["field_type"]=="file")
						{
							$value["file"] = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."upfiles WHERE id='".$rs["field_content"]."'");
							$value["file_en"] = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."upfiles WHERE id='".$rs["field_content_en"]."'");
							$value["file_ft"] = $this->DB->qgGetOne("SELECT * FROM ".$this->prefix."upfiles WHERE id='".$rs["field_content_ft"]."'");
						}
					}
					
					//复选框添加选中状态
					if($value["field_type"]=="checkbox")
					{
						$options_arr = explode(",",$rs["field_content"]);
						foreach($value["options"] AS $key2=>$value2)
						{
							if(in_array($value2["inputval"],$options_arr))
							{
								$value2["checked"] = true;
							}
							$value["options"][$key2] = $value2;
						}
					}
					
					$value["field_content"] = $rs["field_content"];
					$value["field_content_en"] = $rs["field_content_en"];
					$value["field_content_ft"] = $rs["field_content_ft"];
					
					//编辑器内容转化为可编译代码
					if($value["field_type"]=="editor")
					{
						$value["field_content"] = html_entity_decode($value["field_content"]);
						$value["field_content_en"] = html_entity_decode($value["field_content_en"]);
						$value["field_content_ft"] = html_entity_decode($value["field_content_ft"]);
					}
					
					unset($rs);
					$fieldlist[] = $value;
				}
				return $fieldlist;
			}
		}
		else
		{
			return $fieldarray;
		}
	}
	
	//分类第一个主题id 
	function rscateid($cateid)
	{
		if($cateid)
		{
			$sql = "SELECT id FROM ".$this->prefix."msg WHERE cateid='".$cateid."' AND additonid=0 ORDER BY allsortid ASC";
			$rs = $this->DB->qgGetOne($sql);
			return $rs;
		}
	}
	
	//单页信息
	function rs($id)
	{
		$bi = $this->bi;
		$sql = "SELECT m.*,c.catename,c.catename_en,c.catename_ft,c.rootid,c.sysgroupid,c.parentid,c.tpl_msg,s.sign AS sysgroup_sign,s.is_tplfile FROM ".$this->prefix."msg AS m,".$this->prefix."category AS c,".$this->prefix."sysgroup AS s WHERE m.id='".$id."' AND m.cateid=c.id AND c.sysgroupid=s.id AND m.ifcheck=1";
		$rs = $this->DB->qgGetOne($sql);
		if($rs)
		{
			$rs["subject"] = $rs["subject".$bi];
			
			//月份转换
			$rs["day"] = date("d",$rs["postdate"]);
			$rs["month"] = monthc(date("m",$rs["postdate"]),"min"); //[min] or [ful]
			$rs["year"] = date("Y",$rs["postdate"]);
			$rs["postdate"] = date("Y-m-d",$rs["postdate"]);
			
			//添加图片
			if($rs["thumb"] || $rs["thumb2"] || $rs["thumb3"])
			{
				$rs = $this->add_thumbarr($rs);
			}
			//内容
			$sql = "SELECT * FROM ".$this->prefix."msg_content WHERE id='".$id."'";
			$rsc = $this->DB->qgGetOne($sql);
			$rs["content"] = $rsc["content".$bi];
			$rs["content_ft"] = $rsc["content_ft"];
			$rs["content_en"] = $rsc["content_en"];
			
			//获取自定义字段
			$rs["fieldlist"] = $this->fieldlist($rs["id"]);
			if($rs["fieldlist"])
			{
				foreach($rs["fieldlist"] AS $key2=>$value2)
				{
					$rs[$value2["sign"]] = $value2["field_content"];
				}
			}
			return $rs;
		}
	}
	
	//上一篇下一篇
	function prne($id,$act)
	{
		$bi = $this->bi;
		$rs = $this->rs($id);
		if($act=="lone")
		{
			$sql = "SELECT id,subject,subject_en,subject_ft,sortid FROM ".$this->prefix."msg WHERE sortid >'".$rs["sortid"]."' AND cateid= ".$rs["cateid"]." AND additonid=0 AND ifcheck=1 ORDER BY sortid ASC LIMIT 1 ";
		}
		elseif($act=="none")
		{
			$sql = "SELECT id,subject,subject_en,subject_ft,sortid FROM ".$this->prefix."msg WHERE sortid <'".$rs["sortid"]."' AND cateid= ".$rs["cateid"]." AND additonid=0 AND ifcheck=1 ORDER BY sortid DESC LIMIT 1";
		}
		$msg = $this->DB->qgGetOne($sql);
		$msg["subject"] = $msg["subject".$bi];
		return $msg;
	}
	
	//取得主题的分类信息
	/*function get_msgcate($id)
	{
		$rs = $this->rs($id);
		$rs = $this->thiscate($rs["cateid"]);
		return $rs;
	}*/
	
	//分类信息
	function rscate($cateid)
	{
		$sql = "SELECT * FROM ".$this->prefix."category WHERE id='".$cateid."'";
		$rs = $this->DB->qgGetOne($sql);
		$rs["catename"] = $rs["catename".$this->bi];
		return $rs;
	}
	
	//根分类信息
	function rootcate($cateid)
	{
		//基本信息
		$rootcate = $this->getroot($cateid);
		//添加图片
		$rootcate = $this->add_thumbarr($rootcate);
		return $rootcate;
	}
	
	//取得根分类
	function getroot($cateid)
	{
		$rs = $this->rscate($cateid);
		if($rs["parentid"]==0)
		{
			$result = $rs;
		}
		else
		{
			$result = $this->getroot($rs["parentid"]);
		}
		return $result;
	}
	
	//往一维数组里添加图片参数
	function add_thumbarr($array)
	{
		if($array["thumb"])
		{
			$imgarr = get_thumbarr($array["thumb"]);
			if($imgarr)
			{
				foreach($imgarr AS $key=>$value)
				{
					$array[$key] = $value;
				}
			}
		}
		if($array["thumb2"])
		{
			$imgarr = get_thumbarr($array["thumb2"]);
			if($imgarr)
			{
				foreach($imgarr AS $key=>$value)
				{
					$array[$key."2"] = $value;
				}
			}
		}
		if($array["thumb3"])
		{
			$imgarr = get_thumbarr($array["thumb3"]);
			if($imgarr)
			{
				foreach($imgarr AS $key=>$value)
				{
					$array[$key."3"] = $value;
				}
			}
		}
		return $array;
	}
	
	//获取系统组下的所有分类
	function sysgroup_cate($id)
	{
		$sql = "SELECT id FROM ".$this->prefix."category WHERE sysgroupid='".$id."' AND status='1'";
		$idlist = $this->DB->qgGetAll($sql);
		if($idlist)
		{
			foreach($idlist AS $key=>$value)
			{
				$id_array[] = $value["id"];
			}
			$idin = implode(",",$id_array);
		}
		return $idin;
	}
}
?>